//! @Author: DengLibin 
//! @Date:  2022-04-21 10:10:38
//! @Description: 路由配置
use actix_web::web;
use actix_web::Scope;
use crate::controller::part_controller;
use crate::controller::product_controller;
use crate::controller::json_controller;
use crate::config::app_data;
/// @Author: DengLibin 
/// @Date:  2022-04-21 11:56:40
/// @Description: 路由配置
fn route_config(cfg: &mut web::ServiceConfig) {
    info!("注册controller路由");
    // domain includes: /products/{product_id}/parts/{part_id}
    let mut services: Vec<Scope> = vec![];

    services.push(product_controller::route());
    services.push(part_controller::route());
    services.push(json_controller::route());

    for s in services {
        cfg.service(s);
    }
    info!("注册controller路由---完成")
    //嵌套路由
    // cfg.service(web::scope("/products")
    //         .service(
    //             web::resource("")
    //                 .route(web::get().to(product_controller::get_products))
    //                 .route(web::post().to(product_controller::add_product)),
    //         )
    //         .service(
    //             web::scope("/{product_id}")
    //                 .service(
    //                     web::resource("")
    //                         .route(web::get().to(product_controller::get_product_detail))
    //                         .route(web::delete().to(product_controller::remove_product)),
    //                 )
    //                 .service(
    //                     web::scope("/parts")
    //                         .service(
    //                             web::resource("")
    //                                 .route(web::get().to(part_controller::get_parts))
    //                                 .route(web::post().to(part_controller::add_part)),
    //                         )
    //                         .service(
    //                             web::resource("/{part_id}")
    //                                 .route(web::get().to(part_controller::get_part_detail))
    //                                 .route(web::delete().to(part_controller::remove_part)),
    //                         ),
    //                 ),
    //         ),
    // );
}

/// @Author: DengLibin 
/// @Date:  2022-04-22 16:37:29
/// @Description: json配置
fn json_config(cfg: &mut web::ServiceConfig){
    //<- limit size of the payload (global configuration)
    cfg.app_data(web::JsonConfig::default().limit(4096))
       .app_data(web::JsonConfig::default().error_handler(app_data::json_error_handler));

}

/// @Author: DengLibin 
/// @Date:  2022-04-22 16:37:34
/// @Description: app配置
pub fn config_app(cfg: &mut web::ServiceConfig) {
    route_config(cfg);
    json_config(cfg);
    
}
